package jmvm.components;

import java.io.*;

public class MicroMemory extends LogicalComponent {

	private String [] memory;
	
	public MicroMemory(int size) {
		super(1,3,false);
		
		out[0].setIsInt();
		out[0].setNumOfLines(8);
		out[1].setIsInt();
		out[1].setNumOfLines(8);
		out[2].setIsInt();
		out[2].setNumOfLines(8);
		
		memory = new String[size];
	}
	
	public void func() {
		out[0].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(0,2), 16 ));
		out[1].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(2,4), 16 ));
		out[2].setIntVal( Integer.parseInt( memory[in[0].getIntVal()].substring(4,6), 16 ));
	}
	
	public void init(String fileName ){

		int counter=0;
		BufferedReader reader=null;
		try {
			reader = new BufferedReader(new FileReader(fileName));
			String line = null;
			while ((line = reader.readLine()) != null) {
			 memory[counter++] = line;   
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(reader!=null)
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
	}
	
	public static void main(String[] args){
		
		MicroMemory mm = new MicroMemory(30);
		mm.init("./src/MicroMemoryContent");

		
		mm.setInputPin(0, new Pin(1,8));
		
		mm.func();

		System.out.println( Integer.toBinaryString(mm.getOut(0).getIntVal()) );
		System.out.println( Integer.toBinaryString(mm.getOut(1).getIntVal()) );
		System.out.println( Integer.toBinaryString(mm.getOut(2).getIntVal()) );
		
	}


}
